﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsVoteIp
    {
        private readonly KeRui.DAL.CmsVoteIp dal = new KeRui.DAL.CmsVoteIp();

        public void Add(KeRui.Model.CmsVoteIp model)
        {
            this.dal.Add(model);
        }

        public int CheckVote(int VoteID, int Limint, string AddressIP, int UserId)
        {
            return this.dal.CheckVote(VoteID, Limint, AddressIP, UserId);
        }

        public List<KeRui.Model.CmsVoteIp> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsVoteIp> list = new List<KeRui.Model.CmsVoteIp>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsVoteIp item = new KeRui.Model.CmsVoteIp();
                    if (dt.Rows[i]["ID"].ToString() != "")
                    {
                        item.ID = int.Parse(dt.Rows[i]["ID"].ToString());
                    }
                    if (dt.Rows[i]["VoteID"].ToString() != "")
                    {
                        item.VoteID = int.Parse(dt.Rows[i]["VoteID"].ToString());
                    }
                    if (dt.Rows[i]["UserID"].ToString() != "")
                    {
                        item.UserID = int.Parse(dt.Rows[i]["UserID"].ToString());
                    }
                    item.UserName = dt.Rows[i]["UserName"].ToString();
                    item.CreateIP = dt.Rows[i]["CreateIP"].ToString();
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int IpAddressID)
        {
            this.dal.Delete(IpAddressID);
        }

        public bool Exists(int IpAddressID)
        {
            return this.dal.Exists(IpAddressID);
        }

        public int GetCount(int VoteID)
        {
            string strWhere = "";
            if (VoteID != 0)
            {
                strWhere = "VoteID=" + VoteID;
            }
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetCount("CmsVoteIp", strWhere);
        }

        public List<KeRui.Model.CmsVoteIp> GetList(int PageSizes, int PageIndexs, int VoteID)
        {
            string strWhere = "";
            if (VoteID != 0)
            {
                strWhere = "VoteID=" + VoteID;
            }
            string strOrder = " CreateTime desc";
            int pageSize = PageSizes;
            int pageIndex = PageIndexs;
            DataSet set = new KeRui.DAL.Common().GetList("CmsVoteIp", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsVoteIp> GetListByCache(int PageSizes, int PageIndexs, string strCondition)
        {
            string cacheKey = "CmsVoteIpList-" + strCondition;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(PageSizes, PageIndexs, int.Parse(strCondition));
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.CmsVoteIp>) cache;
        }

        public KeRui.Model.CmsVoteIp GetModel(int VoteID, string ip)
        {
            return this.dal.GetModel(VoteID, ip);
        }

        public void Update(KeRui.Model.CmsVoteIp model)
        {
            this.dal.Update(model);
        }
    }
}

